package com.googlecode.spruce.web.admin;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.googlecode.spruce.dao.HostDao;
import com.googlecode.spruce.dao.ServiceResultDao;
import com.googlecode.spruce.domain.Host;
import com.googlecode.spruce.domain.ServiceResult;
import com.googlecode.spruce.web.admin.dto.MainDashboardDto;

@Controller
@RequestMapping("/admin")
public class MainDashboard
{
    @Autowired
    private HostDao hostDao;

    @Autowired
    private ServiceResultDao serivceResultDao;

    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public ModelAndView getDashBoard()
    {
        final ModelAndView model = new ModelAndView("dashboard");

        final List<Host> hosts = hostDao.getAll();
        final List<MainDashboardDto> dtoList = new ArrayList<MainDashboardDto>();

        for (final Host host : hosts)
        {
            final ServiceResult lastError = serivceResultDao.getLastError(host);
            if (null != lastError)
            {
                dtoList.add(new MainDashboardDto(host, lastError));
            }
        }

        model.addObject("dtoList", dtoList);

        return model;
    }
}
